package com.yourname.bank_sim.list;

import com.yourname.bank_sim.entity.Routes;
import com.yourname.bank_sim.entity.Station;

import java.util.ArrayList;

public class CustomList<E> extends ArrayList<E> {
    public int indexOfById(String id){
        for (int i=0;i<this.size();i++){
            E e = this.get(i);
            if (e instanceof Station){
                if (((Station) e).getId().equals(id)){
                    return i;
                }
            }else if (e instanceof Routes){
                if (((Routes) e).getId().equals(id)){
                    return i;
                }
            }else{
                System.out.println("类型不正确");
                return -1;
            }
        }
        return -1;
    }
    public E getObjectById(String id, Object type){
        if (type instanceof Station){
            for (E e:this){
                Station a=(Station) e;
                if (a.getId().equals(id)){
                    return (E) a;
                }
            }
            System.out.println("没有编号为"+id+"的车站。");
        }else if(type instanceof Routes){
            for (E e:this){
                Routes a=(Routes) e;
                if (a.getId().equals(id)){
                    return (E) a;
                }
            }
            System.out.println("编号为"+id+"的线路不存在");
        }else{
            System.out.println("类型不正确");
            return null;
        }
        return null;
    }
    public boolean updateById(String id, E newObject){
        if (newObject instanceof Station){
            int i = indexOfById(id);
            if (i!=-1){
                this.set(i,newObject);
                return true;
            }else{
                System.out.println("没有编号为"+id+"的车站。");
                return false;
            }
        }else if(newObject instanceof Routes){
            int i = indexOfById(id);
            if (i!=-1){
                this.set(i,newObject);
                return true;
            }else{
                System.out.println("编号为"+id+"的线路不存在。");
                return false;
            }
        }else {
            System.out.println("类型不正确");
            return false;
        }
    }
    public boolean deleteById(String id, E newObject){
        if (newObject instanceof Station){
            int i = indexOfById(id);
            if (i!=-1){
                this.remove(i);
                return true;
            }else{
                System.out.println("没有编号为"+id+"的车站。");
                return false;
            }
        }else if(newObject instanceof Routes){
            int i = indexOfById(id);
            if (i!=-1){
                this.remove(i);
                return true;
            }else{
                System.out.println("编号为"+id+"的车站不存在。");
                return false;
            }
        }else {
            System.out.println("类型不正确");
            return false;
        }
    }

}
